package com.example.network_lib;

import com.example.network_lib.module.base_module.BaseResponse;
import com.example.base.utils.LogUtil;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author Administrator
 * @createTime 2019/9/25 10:54
 */
public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {


    protected RxAppCompatActivity mContext;

    @Override
    public void onSubscribe(Disposable d) {
        LogUtil.d("onSubscribe");
    }

    @Override
    public void onNext(BaseResponse<T> baseResponse) {
        if (baseResponse.getCode() == 200) {
            onSuccess(baseResponse.getData());
        } else {
            onFailure(null, baseResponse.getCode(), baseResponse.getMessage());
        }
    }

    @Override
    public void onError(Throwable e) {
        LogUtil.d(e.getMessage());
        onFailure(e, -1, RxExceptionUtil.exceptionHandler(e));
    }

    @Override
    public void onComplete() {
        LogUtil.d("onComplete");
    }


    protected abstract void onSuccess(T result);

    protected abstract void onFailure(Throwable e, int code, String errMsg);
}
